package com.example.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.entity.WebPage1Block;
import com.example.demo.entity.WebPage1User;
import com.example.demo.entity.WebPage2User;
import com.example.demo.mapper.WebPage1UserMapper;
import com.example.demo.mapper.WebPage2UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/webpage2")
//碰到user
public class Page2Controller {
    @Resource
    WebPage2UserMapper webPage2UserMapper;

    @PostMapping
    public Result<?> save(@RequestBody WebPage2User webPage2User){
        if(webPage2User.getDate()==null){
            webPage2User.setDate(new Date());;
        }
        webPage2UserMapper.insert(webPage2User);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody WebPage2User webPage2User){
        if(webPage2User.getDate()==null){
            webPage2User.setDate(new Date());;
        }
        webPage2UserMapper.updateById(webPage2User);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") int searchPageId){
        LambdaQueryWrapper<WebPage2User>wrapper= Wrappers.<WebPage2User>lambdaQuery();
        wrapper.eq(WebPage2User::getPageId,searchPageId);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(WebPage2User::getTitle,search).or().like(WebPage2User::getAuthor,search).or().eq(
                    WebPage2User::getType,search
            );
        }
        Page<WebPage2User> userPage=webPage2UserMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);
        return Result.success(userPage);
    }
    @GetMapping("/download/{id}")
    public WebPage2User selectById(@PathVariable Long id){
        return webPage2UserMapper.selectById(id);
    }
    @DeleteMapping("/{id}")
    public Result<?> update (@PathVariable Long id){
        webPage2UserMapper.deleteById(id);
        return Result.success();
    }

}
